<template>
  <div class="index">
    <head-text titleText="申请管理"></head-text>
    <div class="list-content">
      <div class="filter-area">
        <Search :searchData="searchData" :searchItem="searchItem"></Search>
        <div>
          <el-button @click="add" plain type="success" size="mini"
            >添加申请</el-button
          >
          <el-button @click="exportApply" plain type="success" size="mini"
            >导出</el-button
          >
        </div>
      </div>
      <div class="table-title">
        <span></span>
        <h5>申请列表</h5>
      </div>
      <custom-table
        :tableData="tableData"
        :tableListData="tableListData"
        :tableBtn="tableBtn"
        :currentData="currentData"
        @onPageChange="onPageChange"
        @onSizeChange="onSizeChange"
        @handleEdit="handleEdit"
        @handlPaySetting="handlPaySetting"
        @handleDelete="handleDelete"
        @handleViewOther="handleViewOther"
        v-if="pageReset"
      ></custom-table>
    </div>
    <dialog-com :dialogObj="dialogObj" @reflash="getList"></dialog-com>
    <dialog-record-list :dialogPayObj="dialogPayObj" @reflash="getList">
    </dialog-record-list>
  </div>
</template>
<script>
import customTable from "@c/table";
import Search from "@c/search";
import headText from "@c/headText";
import dialogCom from "./dialogCom";
import dialogRecordList from "./dialogRecordList";
import timeFormat from "@u/timeFormat";
import {
  getApplyManagePageApi,
  getCustomerListApi,
  deleteApplyManageApi,
  exportApplyManageApi,
  applyCategoryListApi
} from "@s/api";
import {
  APPLYSTATUSENUM,
  APPLYCATEGORYENUM,
  APPLYTYPEENUM,
  APPLYSTATUSLIST,
  ISNOTLIST
} from "@u/wordbook";
export default {
  name: "index",
  data() {
    return {
      nowPage: 1,
      total: 0,
      limit: 10,
      pageReset: true,
      searchItem: [],
      searchData: {
        customerId: "",
        orderNumber: "",
        applyCategory: "",
        title: "",
        applyNumber: "",
        agent: "",
        contractNumber: "",
        accountTransfer: "",
        applyStartDatetime: "",
        applyEndDatetime: "",
        registerStartDate: "",
        registerEndDate: "",
        Status: ""
      },
      dialogObj: {
        id: "",
        title: "",
        show: false,
        form: {
          title: "",
          type: ""
        }
      },
      dialogPayObj: {
        id: "",
        title: "",
        show: false,
        form: {
          title: "",
          type: ""
        }
      },
      importPatentObj: {
        id: "",
        title: "",
        show: false,
        form: {
          title: "",
          type: ""
        }
      },
      importFeeObj: { 
        id: "",
        title: "",
        show: false,
        form: {
          title: "",
          type: ""
        }
      },
      tableData: [],
      customerSelect: [],
      statusSelect: APPLYSTATUSLIST,
      isNotList: ISNOTLIST,
      applyCategoryList: [],
      currentData: {}
    };
  },
  components: {
    headText,
    dialogCom,
    dialogRecordList,
    Search,
    customTable
  },
  created() {
    // form搜索
    this.searchItem = [
      {
        type: "select",
        label: "客户名称：",
        prop: "customerId",
        placeholder: "请选择",
        selectList: this.customerSelect
      },
      {
        type: "select",
        label: "分类：",
        prop: "applyCategory",
        placeholder: "请选择",
        selectList: this.applyCategoryList
      },
      {
        type: "daterange",
        label: "立案日：",
        timeFormat: "yyyy-MM-dd",
        timeList: ["registerStartDate", "registerEndDate"]
      },
      {
        type: "input",
        label: "案卷号",
        prop: "orderNumber",
        placeholder: "请输入案卷号"
      },
      {
        type: "input",
        label: "代理人",
        prop: "agent",
        placeholder: "请输入代理人"
      },
      {
        type: "input",
        label: "申请名称",
        prop: "title",
        placeholder: "请输入申请名称"
      },
      {
        type: "input",
        label: "申请号",
        prop: "applyNumber",
        placeholder: "请输入申请号"
      },
      {
        type: "daterange",
        label: "申请日：",
        timeFormat: "yyyy-MM-dd",
        timeList: ["applyStartDatetime", "applyEndDatetime"]
      },
      {
        type: "input",
        label: "合同号",
        prop: "contractNumber",
        placeholder: "请输入合同号"
      },
      {
        type: "select",
        label: "是否到账：",
        prop: "accountTransfer",
        placeholder: "请选择",
        selectList: this.isNotList
      },
      {
        type: "select",
        label: "状态：",
        prop: "status",
        placeholder: "请选择",
        selectList: this.statusSelect
      }
    ];
    //  table表格
    this.tableListData = [
      { width: "50", label: "", type: "index" },
      { prop: "customerName", width: "200px", label: "客户名称", type: "a" },
      { prop: "orderNumber", width: "160px", label: "案卷号", type: "a" },
      { prop: "contractNumber", width: "160px", label: "合同编号", type: "a" },
      {
        prop: "applyCategory",
        width: "90px",
        label: "分类",
        type: "wordbook",
        wordbookList: this.getApplyCategory
      },
      {
        prop: "applyType",
        width: "90px",
        label: "类型",
        type: "wordbook",
        wordbookList: this.getApplyType
      },
      { prop: "title", width: "230px", label: "申请名称" },
      { prop: "agentName", width: "90px", label: "代理人" },
      { prop: "applyNumber", width: "150px", label: "申请号" },
      { prop: "applyDatetime", width: "150px", label: "申请日期" },
      {
        prop: "status",
        width: "130px",
        label: "状态",
        type: "wordbook",
        wordbookList: this.getApplyStatus
      },
      { prop: "fileNames", width: "500px", label: "已上传文件", type: "a" },
      { label: "操作", type: "btn", width: "150", fixed: "right" }
    ];
    // 按钮
    this.tableBtn = [
      {
        name: "编辑",
        btnType: "primary",
        handleFn: "handleEdit"
      },
      {
        name: "删除",
        btnType: "danger",
        handleFn: "handleDelete"
      },
      {
        name: "申请操作记录",
        btnType: "primary",
        handleFn: "handlPaySetting"
      }
    ];
  },
  mounted() {
    this.getCustomerSelect();
    this.applyCategoryListHandle();
    this.getList();
  },
  methods: {
    // 缴费金额
    getFeeAmount(val) {
      if (val) {
        return timeFormat.formatFloat(val / 100, 2);
      } else {
        return "--";
      }
    },
    getApplyCategory(val) {
      return APPLYCATEGORYENUM[val];
    },
    getApplyType(val) {
      return APPLYTYPEENUM[val];
    },
    getApplyStatus(val) {
      return APPLYSTATUSENUM[val];
    },
    exportApply() {
      this.$confirm(
        "按照查询条件进行导出，是否导出?",
        "提示",
        {
          confirmButtonText: "是",
          cancelButtonText: "否",
          type: "warning"
        }
      ).then(() => {
        let json = JSON.parse(JSON.stringify(this.searchData));
        let now = new Date();
        var year = now.getFullYear();
        var month = now.getMonth() + 1;
        var day = now.getDate();
        if (month >= 1 && month <= 9) {
          month = "0" + month;
        }
        if (day >= 0 && day <= 9) {
          day = "0" + day;
        }
        var fileName = "申请管理_" + year + month + day;
        exportApplyManageApi(json, fileName).then(() => {});
      });
    },
    // 查看
    handleViewOther(row) {
      this.dialogObj.id = row.id;
      this.dialogObj.read = true;
      this.dialogObj.show = true;
      this.dialogObj.title = "查看申请";
      this.dialogObj.form = row;
    },
    // 缴费设定
    handlPaySetting(row) {
      this.dialogPayObj.id = row.id;
      this.dialogPayObj.show = true;
      this.dialogPayObj.form = row;
      this.dialogPayObj.title = "申请操作记录";
    },
    // 编辑
    handleEdit(row) {
      this.dialogObj.id = row.id;
      this.dialogObj.read = false;
      this.dialogObj.show = true;
      this.dialogObj.form = row;
      this.dialogObj.title = "编辑申请";
    },
    // 删除
    handleDelete(row) {
      this.$confirm("此操作将删除该申请?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let json = row.id; 
        deleteApplyManageApi(json).then(() => {
          this.getList();
          this.$message({
            type: "success",
            message: "删除成功！"
          });
        });
      });
    },
    onPageChange(val) {
      this.nowPage = val;
      this.getList();
    },
    onSizeChange(val) {
      this.nowPage = 1;
      this.limit = val;
      this.getList();
    },
    // 搜索事件
    getDataList(val) {
      this.searchData = val;
      this.nowPage = 1;
      this.limit = 10;
      this.pageReset = false;
      this.$nextTick(() => {
        this.getList();
        this.pageReset = true;
      });
    },
    //获取列表
    getList() {
      let json = JSON.parse(JSON.stringify(this.searchData));
      json.page = this.nowPage;
      json.limit = this.limit;
      getApplyManagePageApi(json).then(res => {
        this.tableData = res.list;
        this.currentData.total = res.total;
      });
    },
    // 获取客户列表
    getCustomerSelect() {
      getCustomerListApi().then(res => {
        let newCustomerList = [];
        res.forEach(item => {
          var newItem = {
            id: item.id,
            value: item.customerName
          };
          newCustomerList.push(newItem);
        });
        this.customerSelect = newCustomerList;
        this.searchItem.forEach(item => {
          if (item.prop == "customerId") {
            item.selectList = this.customerSelect;
          }
        });
      });
    },
     applyCategoryListHandle() {
      applyCategoryListApi().then(res => {
        let newApplyCategoryList = [];
         res.forEach(item => {
          var newSettingItem = {
            id: item.settingValue,
            value: item.settingCode
          };
          newApplyCategoryList.push(newSettingItem);
        });
        this.applyCategoryList = newApplyCategoryList;
        this.searchItem.forEach(item => {
          if (item.prop == "applyCategory") {
            item.selectList = this.applyCategoryList;
          }
        });
      })
    },
    add() {
      this.dialogObj.id = "";
      this.dialogObj.read = false;
      this.dialogObj.show = true;
      this.dialogObj.title = "添加申请";
    }
  }
};
</script>
<style scoped lang="scss"></style>
